const CodeInstrumentation = require('../../models/mongo/code-instrumentation');
const ProjectVersion = require('../../models/mongo/project-version');
const Project = require('../../models/mongo/project');
const PhoenixError = require('../../models/phoenix-error');
function requireCodeInstrumentationExist(req, res, next) {
  (async () => {
    const { codeInstrumentationId } = req.params;
    if (!codeInstrumentationId) {
      next(new PhoenixError('invalid request', "need 'codeInstrumentationId'"));
      return;
    }
    const codeIn = await CodeInstrumentation.findById(codeInstrumentationId);
    if (!codeIn) {
      next(new PhoenixError('not found', `codeInstrumentationId:${codeInstrumentationId} not exist!`));
      return;
    }
    req.$codeInstrumentation = codeIn;
    req.$projectVersion = await ProjectVersion.findById(codeIn.versionId);
    req.$project = await Project.findById(codeIn.projectId);
    next();
  })().catch((err) => {
    next(new PhoenixError(err));
  });
}
module.exports = {
  requireCodeInstrumentationExist,
};
